/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.secretaria.saude.negocio.base.impl;

/**
 *
 * @author Pablo
 */

import com.secretaria.saude.negocio.base.JasperRelatorioService;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;





public class JasperRelatorioServiceImpl implements JasperRelatorioService {

	private static final String CAMINHO_RELATORIO_TEMPLATE = "/com/secretaria/saude/relatorios/%s.jasper";
	private static final Locale LOCALE_RELATORIO = new Locale("pt", "BR");

        //nome do parametro da logo
	public static final String PARAMETRO_LOGO = "logo";

	private DataSource dataSource;

	public JasperRelatorioServiceImpl(final DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public byte[] gerarRelatorio(final String nomeRelatorio, final String nomeModulo
		, final Map<String, Object> parametros
		, final Collection<?> beans) throws JRException {

		adicionarParametrosPadrao(parametros);

		final JasperReport jasperReport = carregarRelatorio(nomeRelatorio);

		final JRDataSource beansDataSource = new JRBeanCollectionDataSource(beans);
		final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, beansDataSource);

		final byte[] pdf = exportarPDF(jasperPrint);

		return pdf;
	}

	@Override
	public byte[] gerarRelatorio(final String nomeRelatorio
		, final String nomeModulo, final Map<String, Object> parametros)
		throws JRException {

		adicionarParametrosPadrao(parametros);

		final JasperReport jasperReport = carregarRelatorio(nomeRelatorio);
		try {
			final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, dataSource.getConnection());
			final byte[] pdf = exportarPDF(jasperPrint);

			return pdf;
		}
		catch (SQLException e) {
			e.printStackTrace();
			throw new JRException(e);
		}
	}

	private void adicionarParametrosPadrao(final Map<String, Object> params) {
		params.put(JRParameter.REPORT_LOCALE, LOCALE_RELATORIO);
		//params.put(PARAMETRO_LOGO, getLogoIS());
                //params.put("parametro", "Titulo do relatorio");
	}

	private InputStream getLogoIS() {
                //TODO: adicionar logo na pasta de imagens
		final InputStream is = getClass().getResourceAsStream("/com/saude/secretaria/imagens/lupa.png");
		return is;
	}

	private JasperReport carregarRelatorio(final String nomeRelatorio) throws JRException {
		final InputStream is = getClass().getResourceAsStream(
			String.format(CAMINHO_RELATORIO_TEMPLATE, nomeRelatorio));
		final JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);

		return jasperReport;
	}

	private byte[] exportarPDF(final JasperPrint jasperPrint) throws JRException {
		final ByteArrayOutputStream os = new ByteArrayOutputStream();

		final JRExporter exportador = new JRPdfExporter();
		exportador.setParameter(JRExporterParameter.CHARACTER_ENCODING, "ISO-8859-1");
		exportador.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		exportador.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
		exportador.exportReport();

		final byte[] pdf = os.toByteArray();
		return pdf;
	}

	@Override
	public byte[] gerarRelatorioExcel(final String nomeRelatorio, final String nomeModulo
		, final Map<String, Object> parametros
		, final Collection<?> beans) throws JRException {

		//adicionarParametrosPadrao(parametros);

		final JasperReport jasperReport = carregarRelatorio(nomeRelatorio);

		final JRDataSource beansDataSource = new JRBeanCollectionDataSource(beans);
		final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, beansDataSource);

		final byte[] excell = exportarExcel(jasperPrint);

		return excell;
	}

	@Override
	public byte[] gerarRelatorioExcel(final String nomeRelatorio
		, final String nomeModulo, final Map<String, Object> parametros)
		throws JRException {

		//adicionarParametrosPadrao(parametros);

		final JasperReport jasperReport = carregarRelatorio(nomeRelatorio);
		try {
			final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, dataSource.getConnection());
			final byte[] excell = exportarExcel(jasperPrint);

			return excell;
		}
		catch (SQLException e) {
			e.printStackTrace();
			throw new JRException(e);
		}
	}

	public byte[] exportarExcel(final JasperPrint jasperPrint) throws JRException {
		final ByteArrayOutputStream os = new ByteArrayOutputStream();

		final JRExporter exportador = new JRXlsExporter();
		exportador.setParameter(JRExporterParameter.CHARACTER_ENCODING, "ISO-8859-1");
		exportador.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		exportador.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
		//Configuracoes especificas...
                exportador.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
                exportador.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                exportador.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                exportador.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
		exportador.exportReport();

		final byte[] xls = os.toByteArray();
		return xls;
	}


}

